package com.ruoyi.ktv.controller;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.ktv.domain.KtvOrder;
import com.ruoyi.ktv.domain.KtvSubOrder;
import com.ruoyi.ktv.domain.vo.RoomsAndLobbyInformationVo;
import com.ruoyi.ktv.service.IKtvOrderService;
import com.ruoyi.ktv.service.IKtvSubOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 *
 *   大厅包厢主控板Controller
 *
 *   @author xubiao
 *   @date 2023-03-02
 */
@RestController
@RequestMapping("/ktv/centerPanel")
public class KtvCenterPanelController extends BaseController {

    @Autowired
    private IKtvOrderService iKtvOrderService;

    @Autowired
    private IKtvSubOrderService iKtvSubOrderService;
    /**
     * 查询所有包厢和大厅的状态，展示于主控面板
     */
    @GetMapping("/roomsAndLobbyInformation")
    public List<RoomsAndLobbyInformationVo> list(KtvOrder ktvOrderFromVo)
    {
        List<RoomsAndLobbyInformationVo> result = new ArrayList<>();
        if("".equals(ktvOrderFromVo.getOrderSession()) || null == ktvOrderFromVo.getOrderSession()){
            Calendar calendar = Calendar.getInstance();
            StringBuilder orderSession = new StringBuilder();
            orderSession.append(calendar.get(Calendar.YEAR));
            orderSession.append("/");
            orderSession.append(calendar.get(Calendar.MONDAY) + 1);
            orderSession.append("/");
            orderSession.append(calendar.get(Calendar.DAY_OF_MONTH));
            orderSession.append(calendar.get(Calendar.HOUR_OF_DAY)>19?"晚上场":"下午场");
            ktvOrderFromVo.setOrderSession(orderSession.toString());
        }
        KtvOrder ktvOrderSearch  = new KtvOrder();
        ktvOrderSearch.setOrderSession(ktvOrderFromVo.getOrderSession());
        List<KtvOrder> ktvOrderList = iKtvOrderService.selectKtvOrderList(ktvOrderSearch);

        for(KtvOrder ktvOrder : ktvOrderList){
            RoomsAndLobbyInformationVo roomsAndLobbyInformationVo = new RoomsAndLobbyInformationVo();
            roomsAndLobbyInformationVo.setConsumer(ktvOrder.getConsumer());
            roomsAndLobbyInformationVo.setContact(ktvOrder.getConsumerContact());
            roomsAndLobbyInformationVo.setKtvOrder(ktvOrder);
            roomsAndLobbyInformationVo.setMainProductName(ktvOrder.getMainProductName());
            roomsAndLobbyInformationVo.setOrderId(ktvOrder.getOrderId().toString());
            roomsAndLobbyInformationVo.setRoomStatus(ktvOrder.getOrderStatus());
            KtvSubOrder ktvSubOrderSearch = new KtvSubOrder();
            ktvSubOrderSearch.setOrderId(ktvOrder.getOrderId());
            List<KtvSubOrder> ktvSubOrderList = iKtvSubOrderService.selectKtvSubOrderList(ktvSubOrderSearch);
            roomsAndLobbyInformationVo.setKtvSubOrders(ktvSubOrderList);
            Long moneyNow = 0L;
            for(KtvSubOrder ktvSubOrder : ktvSubOrderList){
                if (null != ktvSubOrder.getAmountReceivable()) {
                    moneyNow = moneyNow + ktvSubOrder.getAmountReceivable();
                }
            }
            roomsAndLobbyInformationVo.setMoneyNow(moneyNow.toString());
            result.add(roomsAndLobbyInformationVo);
        }
        return result;
    }

    @GetMapping("/subOrderListByOrder")
    public List<KtvSubOrder> subOrderList(KtvOrder ktvOrderFromVo){
        Long orderId = ktvOrderFromVo.getOrderId();
        KtvSubOrder ktvSubOrderSearch = new KtvSubOrder();
        ktvSubOrderSearch.setOrderId(orderId);
        return iKtvSubOrderService.selectKtvSubOrderList(ktvSubOrderSearch);


    }
}
